
//
//  ZZLostCarAnalysisVC.m
//  新项目
//
//  Created by liGordon on 17/3/13.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZLostCarAnalysisVC.h"
#import "ZZLostCarAnalysisCell.h"
#import "ZZCustomViewModel.h"
#import "ZZLostCarContentModel.h"
#import "ZZLostCarModel.h"
#import "ZZEnterpriseBasicInfoViewModel.h"
#import "ZZLostCarDetailVC.h"

static NSString *identifier = @"LostCarAnalysisCell";

@interface ZZLostCarAnalysisVC ()<UITableViewDelegate,UITableViewDataSource,ZZLostCarAnalysisCellDelegate>
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (weak, nonatomic) IBOutlet UITextField *inputText;
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSString *param1Str;

@end

@implementation ZZLostCarAnalysisVC

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"车辆流失分析";
    
        [self initEnterpriseBasicInfo];
    
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
        [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
            [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];


    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];
    self.inputView.layer.masksToBounds = YES;
    self.inputView.layer.cornerRadius = 8;
    self.searchBtn.layer.masksToBounds = YES;
    self.searchBtn.layer.cornerRadius = 8;


}

-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}
- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 80;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZLostCarAnalysisCell" bundle:nil] forCellReuseIdentifier:identifier];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZLostCarAnalysisCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


#pragma mark -查询企业基础信息
-(void)initEnterpriseBasicInfo
{
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    [[ZZEnterpriseBasicInfoViewModel shareViewModel]getEnterpriseBasicInfoSettingWithCompanyID:loginDict[@"CompanyID"] andCompanyFID:loginDict[@"CompanyFID"] andIsinherit:@"0" andType:@"TXSZ1022" andCode:@"CLLSTX" andSuccess:^(NSDictionary *result) {
        if (result) {
            //NSDictionary *sucDict = [WHJSON objectFormJsonString:[result objectForKey:@"resultJson"]];

                    //NSLog(@"%@",sucDict);
           // NSLog(@"%@",sucDict[@"Name"]);
             //  NSLog(@"%@",sucDict[@"Param1"]);
            NSArray *sucArray = [WHJSON objectFormJsonString:[result objectForKey:@"resultJson"]];
            NSDictionary *sucDict = sucArray.firstObject;
            NSLog(@"%@",sucDict[@"Param1"]);
            self.param1Str = sucDict[@"Param1"];
            if (self.param1Str.length <= 0) {
                self.param1Str = @"30";
            }
            [self initDataWithPageIndex:@"1"];
        }
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };

    }];
}

#pragma mark -查询车辆流失信息
-(void)initDataWithPageIndex:(NSString *)pageIndex
{
    NSLog(@"pageIndex===%@",pageIndex);
    NSString *PageIndex = [NSString stringWithFormat:@"%ld",(long)self.index];
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    
 

    [[ZZCustomViewModel shareViewModel]getCarLostAnalysisWithCompanyID:loginDict[@"CompanyID"]andPageIndex:pageIndex andPageSize:@(upNum)  andNum:self.param1Str andSuccess:^(NSDictionary *result) {
        if (result) {
            
            [ZZLostCarModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"rows" : @"ZZLostCarContentModel",
                         };
            }];
            ZZLostCarModel *fg = [ZZLostCarModel mj_objectWithKeyValues:result[@"resultJson"]];
            
            self.total = [fg.total integerValue];
            
            
            
            if (!ceilf([fg.total floatValue] / 10.00)) {
                NSInteger x = 0;
                self.num = x;
            }else{
                NSInteger x = ceilf([fg.total floatValue] / 10.00);
                self.num = x;
            }
            
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in fg.rows) {
                [mutableArr addObject:dic];
            }
            

            if (self.index!=1) {
                if (self.index > self.num) {
                    [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }else{
                    [self.dataArr addObjectsFromArray: mutableArr];
                }
                [self.menuTable.mj_footer endRefreshing];
            }
            
            else
            {
                self.dataArr=mutableArr;
                [self.menuTable.mj_header endRefreshing];
                
            }
            
            
            
            
            
            
            
            [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            [self.menuTable reloadData];
            
            
        }
        
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };
        
    }];

    
}


-(void)clickDetail:(ZZLostCarContentModel *)model
{
    ZZLostCarDetailVC * vc = [[ZZLostCarDetailVC alloc]init];
    
    vc.model = model;
    PushVC(vc)
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ZZLostCarDetailVC *vc = [[ZZLostCarDetailVC alloc]init];
    vc.model = self.dataArr[indexPath.row];
    PushVC(vc)
    
}
- (IBAction)clickForSelect:(id)sender {
    if ([self.inputText.text integerValue] > 0) {
          self.param1Str = self.inputText.text;
    }else{
        [MBProgressHUD showError:@"天数需大于0"];
    }
   // self.param1Str = self.inputText.text;
    [self.menuTable.mj_header beginRefreshing];
}

@end
